package com.ggb.reactive

import org.springframework.http.server.reactive.HttpHandler
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.http.server.reactive.ServerHttpResponse
import reactor.core.publisher.Mono
import reactor.netty.http.server.HttpServer

class FluxMainApplication

fun main(args: Array<String>) {
    val handler = HttpHandler { request: ServerHttpRequest, response: ServerHttpResponse ->

        val uri = request.uri.toString()
        println(Thread.currentThread().toString() + "请求进来：" + uri)
        val factory = response.bufferFactory()
        val dataBuffer = factory.wrap((uri + "Hello World!").toByteArray())
        val body = Mono.just(dataBuffer)
        response.writeWith(body)
    }

    val adapter = ReactorHttpHandlerAdapter(handler)

    HttpServer.create()
        .host("localhost")
        .port(8080)
        .handle(adapter)
        .bindNow()

    println("服务器启动完毕。。。监听8080，接受请求")
    readln()
    println("服务器停止")


}